{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T10:47:50.757769Z",
     "start_time": "2020-10-21T10:47:50.752062Z"
    }
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "def upickle(file):\n",
    "    fo=open(file,'rb')\n",
    "    dict=pickle.load(fo,encoding='latin1')\n",
    "    fo.close()\n",
    "    return dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T10:56:12.773838Z",
     "start_time": "2020-10-21T10:56:12.639748Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def clean(data):\n",
    "    imgs=data.reshape(data.shape[0],3,32,32)\n",
    "    grayscale_imgs=imgs.mean(1)\n",
    "    cropped_imgs=gray_imgs[:,4:28,4:28]\n",
    "    img_data=cropped_imgs.reshape(data.shape[0],-1)\n",
    "    img_size=np.shape(img_data)[1]\n",
    "    means=np.mean(img_data,axis=1)\n",
    "    meansT=means.reshape(len(means),1)\n",
    "    stds=np.std(img_data,axis=1)\n",
    "    stdsT=stds.reshape(len(stds),1)\n",
    "    adj_stds=np.maximun(stdsT,1.0/np.sqrt(imgs_size))\n",
    "    normalized=(img_data-meanT)/adj_stds\n",
    "    return normalized\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T12:39:57.422327Z",
     "start_time": "2020-10-21T12:39:57.412619Z"
    }
   },
   "outputs": [],
   "source": [
    "def read_data(directory):\n",
    "    names=unpickle('{}/batches.meta'.format(directory))['label_names']\n",
    "    print ('names',names)\n",
    "    \n",
    "    data,labels=[],[]\n",
    "    for i in range(1,6):\n",
    "        filename='{}/data_batch_{}'.format(directory,i)\n",
    "        batch_data=unpickle(filename)\n",
    "        if len(data)>0:\n",
    "            data=np.vstack((data,batch_data['data']))\n",
    "            labels=np.hstack((labels,batch_data['labels']))\n",
    "        else:\n",
    "            data=batch_data['data']\n",
    "            labels=batch_data['labels']\n",
    "            \n",
    "    print(np.shape(data),np.shape(labels))\n",
    "\n",
    "    data=clean(data)\n",
    "    data=data.astype(np.float32)\n",
    "    return names,data,labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-21T12:40:14.315248Z",
     "start_time": "2020-10-21T12:40:14.251604Z"
    }
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'cifar_tools'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                    Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-6edb28a07d2d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mcifar_tools\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'cifar_tools'"
     ]
    }
   ],
   "source": [
    "import cifar_tools\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
